home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / COPYALL.C < prev    next >
Text File  |  1993-06-26  |  5KB  |  223 lines

  1.  
  2. /*
  3.     This program was written to simplify mass file
  4.     transfers on a single-drive disk system (like mine.)
  5.     The following things must be on the disk when you run
  6.     copyall:
  7.         1) the "copyall" program,
  8.         2) the files you wish to transfer (copy),
  9.         3) a text file containing
  10.            the names of all the files to be copied.
  11.  
  12.     *************************************************
  13.     * NOTE: None of the files you try to transfer    *
  14.     *    should be any longer than BUFSIZ bytes    *
  15.     *    in length (copyall will check for this.)*
  16.     *************************************************
  17.  
  18.  
  19.     Example of use:
  20.     If you want to copy the files "FOO.C", "BAR.C", "ZOT.C"
  21.     and "FRAZ.ASM", then first edit a file (say, "CLIST")
  22.     to appear as follows:
  23.  
  24.     -----------------------------
  25.     foo.c <cr>
  26.     bar.c <cr>
  27.     zot.c <cr>
  28.     fraz.asm <cr>
  29.     -----------------------------
  30.  
  31.     The format is: one filename per line, no spaces or tabs anywhere.
  32.     (the dashed lines should NOT be in the file...<cr> means CR-LF.)
  33.  
  34.     Then, to copy the files, just say:
  35.  
  36.     A>copyall <cr>
  37.  
  38.     answer "clist" when it asks you for the name of the
  39.     control file, and let "copyall" do the rest!
  40. */
  41.  
  42. #include "bdscio.h"
  43.  
  44. #define MAXFILES 30
  45.  
  46. char fnames[MAXFILES][13];     /*    file names    */
  47. int nrecs[MAXFILES];    /* # of records in each file    */
  48. int fnc;        /* # of files in currently in buffer    */
  49.  
  50. int ffd;        /* file descriptor for source files    */
  51. char ibuf[BUFSIZ];    /* I/O buffer for filename file */
  52.  
  53. char done, partly;    /*    flags            */
  54.  
  55. char control[20];    /* name of control file        */
  56. int ncopies;        /* number of copies to make    */
  57.  
  58. unsigned bufroom;
  59.  
  60. char *bufp;
  61.  
  62. char buf[0];
  63.  
  64. main()
  65. {
  66.     int fd1,i;    /* file descriptor of file list file */
  67.     done = 0;    /* Not done yet */
  68.     partly = 0;    /* not partly done reading in a file */
  69.  
  70.     bufp = &buf;
  71.     i = &buf;
  72.     bufroom = topofmem() - 300 - i;
  73.  
  74.     printf("BDS Single Disk Mass File Copy Program\n\n");
  75.     printf("How many copies? ");
  76.     ncopies = atoi(gets(control));
  77.  
  78.     printf("Insert source disk (it must contain the\n");
  79.     printf("files to be copied AND the control file\n");
  80.     printf("containing the names of the files to be\n");
  81.     printf("copied. Hit return when source disk is in: ");
  82.     getchr();
  83.     printf("What is the control file named? ");
  84.     gets(control);
  85.     fd1 = fopen(control,ibuf);
  86.     if (fd1 == ERROR) {
  87.         printf("Can't read %s\n",control);
  88.         exit();
  89.     }
  90.  
  91.     while (fillbuf()) {
  92.         dumpbuf();
  93.     }
  94.     printf("All done!\n");
  95.     close(fd1);
  96. }
  97.  
  98.  
  99. /*
  100.     Routine to fill up the memory buffer as much as
  101.     possible. If it fills up in the middle of reading
  102.     in a file, set the "partly" flag and get the rest
  103.     of the file next time. Returns 0 when nothing left
  104.     to read in, else returns 1.
  105. */
  106.  
  107. fillbuf()
  108. {
  109.     int i,ii,nr;
  110.     if (done) return 0;
  111.     if (partly) {
  112.         printf("Loading rest of %s\n",fnames[fnc]);
  113.         movmem(bufp - (nr = nrecs[fnc] * 128), buf,nr);
  114.         strcpy(fnames[0], fnames[fnc]);
  115.         bufp = buf + nr;
  116.         i = 0;
  117.         do {
  118.             if (bufp + 1023 >= buf + bufroom) {
  119.                 printf("\7\nFile too big. Aborting.\n");
  120.                 exit();
  121.              }
  122.             i += (ii = read(ffd, bufp, 8));
  123.             bufp += ii * 128;
  124.         } while (ii == 8);
  125.         nrecs[0] = nrecs[fnc] + i;
  126.         fnc = 1;
  127.         partly = 0;
  128.         close(ffd);
  129.      }
  130.     else {
  131.         bufp = buf;
  132.         fnc = 0;
  133.      }
  134.  
  135.     do {
  136.         partly = 1;
  137.  
  138.         if ( !fscanf(ibuf, "%s\n", &fnames[fnc]) ||
  139.                     fnames[fnc][0] == '\0')
  140.             return done = 1;
  141.  
  142.         ffd = open(fnames[fnc], 0);
  143.         if (ffd == ERROR) {
  144.             printf("\7\nCan't open %s...",fnames[fnc]);
  145.             printf("Skipping that one.\n\n");
  146.             partly = 0;
  147.             continue;
  148.          }
  149.         else printf("Loading %s\n",fnames[fnc]);
  150.  
  151.         nrecs[fnc] = 0;
  152.         while (bufp + 1023 < buf + bufroom) {
  153.            nr = read(ffd, bufp, 8);
  154.            if (nr == ERROR) {
  155.             printf("\7\nRead error. aborting.\n");
  156.             exit();
  157.            }
  158.            bufp += nr * 128;
  159.            nrecs[fnc] += nr;
  160.            if (nr != 8) {
  161.             fnc++;
  162.             close(ffd);
  163.             partly = 0;
  164.             break;
  165.             }
  166.          }
  167.       if (!fnc) {
  168.         printf("\7\nFile too long. Aborting.\n");
  169.         exit();
  170.        }
  171.     } while (!partly);
  172.     return 1;
  173. }
  174.  
  175.  
  176. /*
  177.     This routine dumps fnc files to the destination
  178.     disk:
  179. */
  180.  
  181. dumpbuf()
  182. {
  183.     int fd, i;
  184.     int pass;
  185.     char *bufp;
  186.  
  187.     for (pass=0; pass<ncopies; pass++) {
  188.       printf("\nInsert destination disk #%1d: ",pass+1);
  189.       getchr();
  190.       bdos(13);    /* log in disk so we don't get R/O error */
  191.       bufp = buf;
  192.       for (i=0; i<fnc; i++) {
  193.         if ((fd = creat(fnames[i])) == ERROR) {
  194.             printf("\7\nCan't create %s\n",fnames[i]);
  195.             break;
  196.          }
  197.         else printf("Writing %s\n",fnames[i]);
  198.         if ((write(fd, bufp, nrecs[i])) != nrecs[i]) {
  199.             printf("\7\nWrite error on %s\n",fnames[i]);
  200.             break;
  201.          }
  202.         close(fd);
  203.         bufp += nrecs[i] * 128;
  204.        }
  205.      }
  206.     printf("\nPut back source disk: ");
  207.     getchr();
  208. }
  209.  
  210.  
  211. /*
  212.     Routine to wait for a key to be typed and then
  213.     echo a CRLF:
  214. */
  215.  
  216. getchr()
  217. {
  218.     if (kbhit()) getchar(); /* clear status if kb pre-hit */
  219.     getchar();
  220.     putchar('\n');
  221. }
  222.  
  223.